package i7bac;

import i7bac.menu.MenuBar;
import i7bac.scale.Scale;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	// Window Font
	private static Font font = new Font("Segoe UI", Font.PLAIN, 12);
	// Menu Bar
	private static MenuBar menuBar;
	// Scale, Containers and Log
	private static JPanel scale, containersCard, logCard;
	// Status bar
	private static JLabel status;

	private MainFrame(String title) {
		/* 
		 * Build the main window GUI
		 */
		super(title);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout());
		this.setVisible(true);
		Container content = this.getContentPane();

		// MENU BAR
		setMenuBarActions();
		this.setJMenuBar(menuBar);

		// DOSING PLANT SCALE
		scale = new Scale(500, 2000, font);
		content.add(scale, BorderLayout.WEST);

		// TABBED PANE to build the Containers and the Log
		JTabbedPane tabs = new JTabbedPane();
		tabs.setFont(font);
		tabs.setBorder(BorderFactory.createEmptyBorder(0,0,2,3));
		// DOSING PLANT CONTAINERS
		containersCard = new JPanel(true);
		tabs.addTab("Containers", null, containersCard, null);
		// LOG
		logCard = new JPanel(true);
		tabs.addTab("Log", null, logCard, null);
		content.add(tabs);

		// STATUS BAR
		status = new JLabel("Status", JLabel.RIGHT);
		status.setFont(font);
		content.add(status, BorderLayout.SOUTH);

		// Pack and center
		this.pack();
		this.setSize(new Dimension(640,400));//TODO
		this.setMinimumSize(new Dimension(320,240));//TODO
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((screenSize.width - this.getWidth())/2, (screenSize.height - this.getHeight())/2);
	}

	public static void setMenuBarActions() {
		/*
		 * Define the menu actions by adding Action Listeners
		 * These are set on the main class so it can handle all the events on the application.
		 */
		menuBar = new MenuBar(font);

		/*
		 * FILE MENU
		 * 		New Set (Ctrl-N): Create a new set from zero
		 * 		Open File (Ctrl-O): Open a file containing an existing set
		 * 		Save As (Ctrl-S): Save the current set in a file
		 * 		Recent Files: Up to 4 recent files are shown for faster set load
		 * 		Exit: Close the application
		 */
		menuBar.newSet.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("File -> New Set");
				System.out.println("File -> New Set");
			}
		});

		menuBar.openFile.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("File -> Open File...");
				System.out.println("File -> Open File...");
			}
		});

		menuBar.saveAs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("File -> Save As...");
				System.out.println("File -> Save As...");
			}
		});

		menuBar.exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		/*
		 * EDIT MENU
		 * 		Cut (Ctrl-X): Cut the container selected
		 * 		Copy (Ctrl-C): Copy the container selected
		 * 		Paste (Ctrl-V): Paste a previously copied container
		 * 		Select All (Ctrl-A): Select all the container
		 */
		menuBar.cut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("Edit -> Cut");
				System.out.println("Edit -> Cut");
			}
		});

		menuBar.copy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("Edit -> Copy");
				System.out.println("Edit -> Copy");
			}
		});

		menuBar.paste.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("Edit -> Paste");
				System.out.println("Edit -> Paste");
			}
		});

		menuBar.selectAll.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				status.setText("Edit -> Select All");
				System.out.println("Edit -> Select All");
			}
		});
	}

	public static void main(String[] args) {
		/*
		 * Run the application
		 */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame("Dosing Plant Manager");
            }
        });
    }

}
